<?php
class Section_Model extends CI_Model {
	var $database;
	public function __construct() {
        parent::__construct();
        $this->database = $this->db->conn_id;		
	}
	public function get($intSectionId) {
		$stmt = $this->database->prepare('call asections_get(?)');
		$stmt->bindParam(1, $intSectionId, PDO::PARAM_INT, 11);
        $stmt->execute();
        $result = $stmt->fetch();
        return $result;
	}	
	public function getSection() {
	   $stmt = $this->database->prepare('call asections_getall();');
       $stmt->execute();
	   $arrResult = $stmt->fetchAll();
	   return isset($arrResult) ? $arrResult : null;
	}
    public function getAll($intOffset, $intLimit, &$intTotal=0) {
		$stmt = $this->database->prepare('call asections_getall_a(?,?)');
		$stmt->bindParam(1, $intOffset,   PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intLimit,    PDO::PARAM_INT, 4);
        $stmt->execute();
		$arrResult 	= $stmt->fetchAll();		
		$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;	
		return isset($arrResult) ? $arrResult : null;
	}
    public function add($strCode, $strName, $intPermA) {		
		$stmt = $this->database->prepare('call asections_add(?,?,?)');
		$stmt->bindParam(1, $strCode, 	PDO::PARAM_STR, 50);
		$stmt->bindParam(2, $strName, 	PDO::PARAM_STR, 150);
		$stmt->bindParam(3, $intPermA, 	PDO::PARAM_INT, 4);
        $stmt->execute();
		$arrResult = $stmt->fetch();					
		return isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;		
	}	
	public function update($intSectionId, $strName, $intEnabled, $intPermA) {
		$stmt = $this->database->prepare('call asections_update(?,?,?,?)');
		$stmt->bindParam(1, $intSectionId, 	PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $strName, 		PDO::PARAM_STR, 150);
		$stmt->bindParam(3, $intEnabled, 	PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPermA, 		PDO::PARAM_INT, 4);
		$stmt->execute();        
	}
}